package com.longge;

public class LicenseKeyFormatting {

    public String licenseKeyFormatting(String s, int k) {

        int strCount = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != '-') strCount++;
        }
        int oneStr = strCount % k;
        StringBuilder sb = new StringBuilder();
        String s1 = s.toUpperCase();
        int sum = oneStr != 0 ? oneStr : k;
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != '-') {
                if (sum != 0) {
                    sb.append(s1.charAt(i));
                    sum--;
                } else {
                    sb.append('-');
                    sb.append(s1.charAt(i));
                    sum = k - 1;
                }
            }
        }
        return sb.toString();
    }
}
